﻿using System.Security.Cryptography;
using System.Text;

namespace Service.Utity
{
    public static class StringOption
    {
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="base64IV"></param>
        /// <param name="base64Key"></param>
        /// <returns></returns>
        public static string AesEncrypt(this string source, byte[] iv, byte[] key)
        {
            if (string.IsNullOrEmpty(source))
            {
                return null;
            }
            using Aes aes = Aes.Create();
            aes.IV = iv;
            aes.Key = key;
            ICryptoTransform Encryptor = aes.CreateEncryptor();
            //原字符utf8编码获取byte加密后转换成base64字符串
            byte[] enc = Encryptor.TransformFinalBlock(Encoding.UTF8.GetBytes(source), 0, source.Length);
            return Convert.ToBase64String(enc);
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="base64IV"></param>
        /// <param name="base64Key"></param>
        /// <returns></returns>
        public static string AesDecrypt(this string source, byte[] iv, byte[] key)
        {
            if (string.IsNullOrEmpty(source))
            {
                return null;
            }
            using Aes aes = Aes.Create();
            aes.IV = iv;
            aes.Key =key;
            ICryptoTransform Encryptor = aes.CreateDecryptor();
            //base64字符解码获取原byte[],解密后,通过UTF8编码还原
            byte[] fbString = Convert.FromBase64String(source);
            byte[] enc = Encryptor.TransformFinalBlock(fbString, 0, fbString.Length);
            return Encoding.UTF8.GetString(enc);
        }
        /// <summary>
        /// RSA加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="rsa"></param>
        /// <returns></returns>
        public static string RSAEncrypt(this string source, RSA rsa)
        {
            try
            {
                if (string.IsNullOrEmpty(source))
                {
                    return string.Empty;
                }
                byte[] enc = rsa.Encrypt(Encoding.UTF8.GetBytes(source), RSAEncryptionPadding.Pkcs1);
                return Convert.ToBase64String(enc);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return "-1";
            }
        }

        /// <summary>
        ///  RSA解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="rsa"></param>
        /// <returns></returns>
        public static string RSADecrypt(this string source, RSA rsa)
        {
            try
            {
                if (string.IsNullOrEmpty(source))
                {
                    return string.Empty;
                }
                byte[] dec = rsa.Decrypt(Convert.FromBase64String(source), RSAEncryptionPadding.Pkcs1);
                return Encoding.UTF8.GetString(dec);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return "-1";
            }
        }




        /// <summary>
        /// 混合加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="rsa"></param>
        /// <returns></returns>
        public static string Encrypt(this string source, RSA rsa)
        {
            try
            {
                if (string.IsNullOrEmpty(source))
                {
                    return string.Empty;
                }
                using Aes aes = Aes.Create();
                byte[] key = rsa.Encrypt(aes.Key, RSAEncryptionPadding.Pkcs1);
                byte[] iv = rsa.Encrypt(aes.IV, RSAEncryptionPadding.Pkcs1);
                ICryptoTransform Encryptor = aes.CreateEncryptor();
                //原字符utf8编码获取byte加密后转换成base64字符串
                byte[] enc = Encryptor.TransformFinalBlock(Encoding.UTF8.GetBytes(source), 0, source.Length);
                byte[][] arrays =  { key, iv, enc };
                var res = arrays.SelectMany(array => array).ToArray();
                return Convert.ToBase64String(res);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return "-1";
            }
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="rsa"></param>
        /// <returns></returns>
        public static string Decrypt(this string source, RSA rsa)
        {
            try
            {
                if (string.IsNullOrEmpty(source))
                {
                    return string.Empty;
                }
                var sourceArray = Convert.FromBase64String(source);
                byte[] keySource = new byte[256];
                byte[] ivSource = new byte[256];
                keySource = sourceArray.Take(256).ToArray();
                ivSource = sourceArray.Take(256*2).Skip(256).ToArray();
                var content = sourceArray.Skip(256 * 2).ToArray();
                using Aes aes = Aes.Create();
                aes.Key = rsa.Decrypt(keySource, RSAEncryptionPadding.Pkcs1);
                aes.IV = rsa.Decrypt(ivSource, RSAEncryptionPadding.Pkcs1);
                ICryptoTransform Encryptor = aes.CreateDecryptor();
                byte[] fbString = Encryptor.TransformFinalBlock(content, 0, content.Length);
                return Encoding.UTF8.GetString(fbString);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return "-1";
            }
        }
    }
}
